﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>翻译</title>
<style type="text/css">
#MainTable
{
    width:100%;
    text-align:center;
}
</style>
<script type="text/javascript" src="http://www.google.com/jsapi?v=1&key=ABQIAAAACqGEg_EWjzyatp5DcvOejRR4W90b60fHZvOCS2noQmQiRb84KxQixoel7iEIiOVRigOQjXi2AyU3yQ"></script>
<script language="javascript" type="text/javascript">
    google.load("language", "1");
    function BeginTranslate(isTxtTranslateSourceKeyUp) {
        var _txtTranslateSource = document.getElementById("txtTranslateSource");
        var _txtTranslateReturn = document.getElementById("txtTranslateReturn");

        //为空不翻译
        if (isTxtTranslateSourceKeyUp == true && _txtTranslateSource.value == "") {
            _txtTranslateReturn.value = "";
            return;
        } else if (isTxtTranslateSourceKeyUp == false && _txtTranslateReturn.value == "") {
            _txtTranslateSource.value = "";
            return;
        }
        var OldLanguage = document.getElementById("SelectLanguageOld").value;
        var NewLanguage = document.getElementById("SelectLanguageNew").value;
        if (isTxtTranslateSourceKeyUp == false) {
            var _Temp = OldLanguage;
            OldLanguage = NewLanguage;
            NewLanguage = _Temp;
        }
        
        var _text = "";
        if (isTxtTranslateSourceKeyUp == true) {
            _text = _txtTranslateSource.value;
        } else {
            _text = _txtTranslateReturn.value;
        }

        //检测是哪种语言
        if (OldLanguage == "auto") {
            google.language.detect(_text, function (res) {
                if (!res.error) {
                    OldLanguage = res.language;
                    if (isTxtTranslateSourceKeyUp == true) {
                        document.getElementById("SelectLanguageOld").value = res.language;
                    } else {
                        document.getElementById("SelectLanguageNew").value = res.language;
                    }
                }
            });
        }

        //要翻译成什么语言
        //此语言要明确,如果是自动检测的话,就自动换在中文
        if (NewLanguage == "auto") {
            NewLanguage = "zh-CN";
            if (isTxtTranslateSourceKeyUp == true) {
                document.getElementById("SelectLanguageOld").value = NewLanguage;
            } else {
                document.getElementById("SelectLanguageNew").value = NewLanguage;
            }
        }

        //相同判断
        if (OldLanguage == NewLanguage) {
            if (isTxtTranslateSourceKeyUp == true) {
                _txtTranslateReturn.value = "请选择要翻译为什么语言";
            } else {
                _txtTranslateSource.value = "请选择要翻译为什么语言";
            }
            return;
        }

        //是否支持此种语言的翻译
        if (google.language.isTranslatable(NewLanguage) == false) {
            if (isTxtTranslateSourceKeyUp == true) {
                _txtTranslateReturn.value = "不支持此种语言的翻译";
            } else {
                _txtTranslateSource.value = "不支持此种语言的翻译";
            }
            return;
        }
        
        //开始翻译
        google.language.translate({ "text": _text, "type": google.language.ContentType["TEXT"] }, OldLanguage, NewLanguage, function (result) {
            if (!result.error) {
                var ReturnStr = result.translation;
                if (isTxtTranslateSourceKeyUp == true) {
                    _txtTranslateReturn.value = ReturnStr;
                } else {
                    _txtTranslateSource.value = ReturnStr;
                }
            }
        });
    }
    google.setOnLoadCallback(function () { });
</script>
</head>
<body>
<div>
<table id="MainTable">
    <tbody>
        <tr>
            <td align="right">
                <select id="SelectLanguageOld" onchange="BeginTranslate(false)">
                    <option  value="auto">检测语言</option>
                    <option  value="sq">阿尔巴尼亚语</option>
                    <option  value="ar">阿拉伯语</option>
                    <option  value="az">阿塞拜疆语</option>
                    <option  value="ga">爱尔兰语</option>
                    <option  value="et">爱沙尼亚语</option>
                    <option  value="be">白俄罗斯语</option>
                    <option  value="bg">保加利亚语</option>
                    <option  value="is">冰岛语</option>
                    <option  value="pl">波兰语</option>
                    <option  value="fa">波斯语</option>
                    <option  value="af">布尔文(南非荷兰语)</option>
                    <option  value="da">丹麦语</option>
                    <option  value="de">德语</option>
                    <option  value="ru">俄语</option>
                    <option  value="fr">法语</option>
                    <option  value="tl">菲律宾语</option>
                    <option  value="fi">芬兰语</option>
                    <option  value="ka">格鲁吉亚语</option>
                    <option  value="ht">海地克里奥尔语</option>
                    <option  value="ko">韩语</option>
                    <option  value="nl">荷兰语</option>
                    <option  value="gl">加利西亚语</option>
                    <option  value="ca">加泰罗尼亚语</option>
                    <option  value="cs">捷克语</option>
                    <option  value="hr">克罗地亚语</option>
                    <option  value="lv">拉脱维亚语</option>
                    <option  value="lt">立陶宛语</option>
                    <option  value="ro">罗马尼亚语</option>
                    <option  value="mt">马耳他语</option>
                    <option  value="ms">马来语</option>
                    <option  value="mk">马其顿语</option>
                    <option  value="no">挪威语</option>
                    <option  value="pt">葡萄牙语</option>
                    <option  value="ja">日语</option>
                    <option  value="sv">瑞典语</option>
                    <option  value="sr">塞尔维亚语</option>
                    <option  value="sk">斯洛伐克语</option>
                    <option  value="sl">斯洛文尼亚语</option>
                    <option  value="sw">斯瓦希里语</option>
                    <option  value="th">泰语</option>
                    <option  value="tr">土耳其语</option>
                    <option  value="cy">威尔士语</option>
                    <option  value="uk">乌克兰语</option>
                    <option  value="eu">西班牙的巴斯克语</option>
                    <option  value="es">西班牙语</option>
                    <option  value="iw">希伯来语</option>
                    <option  value="el">希腊语</option>
                    <option  value="hu">匈牙利语</option>
                    <option  value="hy">亚美尼亚语</option>
                    <option  value="it">意大利语</option>
                    <option  value="yi">意第绪语</option>
                    <option  value="hi">印地语</option>
                    <option  value="ur">印度乌尔都语</option>
                    <option  value="id">印尼语</option>
                    <option  value="en">英语</option>
                    <option  value="vi">越南语</option>
                    <option  value="zh-CN" selected="selected">中文</option>
                </select>
            </td>
            <td></td>
            <td align="left">
                <select id="SelectLanguageNew" onchange="BeginTranslate(true)">
                    <option  value="auto">检测语言</option>
                    <option  value="sq">阿尔巴尼亚语</option>
                    <option  value="ar">阿拉伯语</option>
                    <option  value="az">阿塞拜疆语</option>
                    <option  value="ga">爱尔兰语</option>
                    <option  value="et">爱沙尼亚语</option>
                    <option  value="be">白俄罗斯语</option>
                    <option  value="bg">保加利亚语</option>
                    <option  value="is">冰岛语</option>
                    <option  value="pl">波兰语</option>
                    <option  value="fa">波斯语</option>
                    <option  value="af">布尔文(南非荷兰语)</option>
                    <option  value="da">丹麦语</option>
                    <option  value="de">德语</option>
                    <option  value="ru">俄语</option>
                    <option  value="fr">法语</option>
                    <option  value="tl">菲律宾语</option>
                    <option  value="fi">芬兰语</option>
                    <option  value="ka">格鲁吉亚语</option>
                    <option  value="ht">海地克里奥尔语</option>
                    <option  value="ko">韩语</option>
                    <option  value="nl">荷兰语</option>
                    <option  value="gl">加利西亚语</option>
                    <option  value="ca">加泰罗尼亚语</option>
                    <option  value="cs">捷克语</option>
                    <option  value="hr">克罗地亚语</option>
                    <option  value="lv">拉脱维亚语</option>
                    <option  value="lt">立陶宛语</option>
                    <option  value="ro">罗马尼亚语</option>
                    <option  value="mt">马耳他语</option>
                    <option  value="ms">马来语</option>
                    <option  value="mk">马其顿语</option>
                    <option  value="no">挪威语</option>
                    <option  value="pt">葡萄牙语</option>
                    <option  value="ja">日语</option>
                    <option  value="sv">瑞典语</option>
                    <option  value="sr">塞尔维亚语</option>
                    <option  value="sk">斯洛伐克语</option>
                    <option  value="sl">斯洛文尼亚语</option>
                    <option  value="sw">斯瓦希里语</option>
                    <option  value="th">泰语</option>
                    <option  value="tr">土耳其语</option>
                    <option  value="cy">威尔士语</option>
                    <option  value="uk">乌克兰语</option>
                    <option  value="eu">西班牙的巴斯克语</option>
                    <option  value="es">西班牙语</option>
                    <option  value="iw">希伯来语</option>
                    <option  value="el">希腊语</option>
                    <option  value="hu">匈牙利语</option>
                    <option  value="hy">亚美尼亚语</option>
                    <option  value="it">意大利语</option>
                    <option  value="yi">意第绪语</option>
                    <option  value="hi">印地语</option>
                    <option  value="ur">印度乌尔都语</option>
                    <option  value="id">印尼语</option>
                    <option  value="en" selected="selected">英语</option>
                    <option  value="vi">越南语</option>
                    <option  value="zh-TW">中文(繁体)</option>
                    <option  value="zh-CN">中文(简体)</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"><textarea id="txtTranslateSource" rows="8" cols="50"></textarea></td>
            <td>
                <button title="从左到右翻译" onclick="BeginTranslate(true)">>></button>
                <br />
                <br />
                <button title="从右到左翻译" onclick="BeginTranslate(false)"><<</button>
            </td>
            <td align="left"><textarea id="txtTranslateReturn" rows="8" cols="50"></textarea></td>
        </tr>
    </tbody>
</table>
</div>
<script language="javascript" type="text/javascript">
    var timer;
    document.getElementById("txtTranslateSource").onkeyup = function () {
        if (timer != null) {
            clearTimeout(timer);
        }
        timer = setTimeout("BeginTranslate(true)", 1000);
    };
    document.getElementById("txtTranslateReturn").onkeyup = function () {
        if (timer != null) {
            clearTimeout(timer);
        }
        timer = setTimeout("BeginTranslate(false)", 1000);
    };
</script>
</body>
</html>
